﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TCHSHue.BLL;
namespace TCHSHue.Modules.AdminControl.SlideAnh
{
    public partial class ctrSlideImageList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                DisplaySlideImage();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                // Get the HttpFileCollection
                HttpFileCollection hfc = Request.Files;
                var nhanThongbao = "Bạn chưa chọn file để tải lên";
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0 && hpf.ContentLength<5000000)
                    {
                        if (hpf.ContentType == "image/png" || hpf.ContentType == "image/jpeg")
                        {
                            hpf.SaveAs(Server.MapPath("~/Upload/SlideImage") + "/" +
                              System.IO.Path.GetFileName(hpf.FileName));

                            nhanThongbao += string.Format("<b>File: </b>{0}<b> Size: </b>{1}<b> Kiểu:</b> {2} <b style='color:red'>đã tải lên thành công</b><br/>", hpf.FileName, hpf.ContentLength, hpf.ContentType);
                        }
                        else
                            nhanThongbao += string.Format("<b>File: </b>{0}<b> Size: </b>{1}<b> Kiểu:</b> {2} <b style='color:red'>đã tải lên không thành công</b><br/>", hpf.FileName, hpf.ContentLength, hpf.ContentType);

                    }
                }
                thongBao.Visible = true;
                lbThongbao.InnerHtml = nhanThongbao;
                DisplaySlideImage();
            }
            catch (Exception ex)
            {

            }
        }
        void DisplaySlideImage()
        {
            DirectoryInfo diFiles = new DirectoryInfo(Server.MapPath("~/Upload/SlideImage"));
            var listFile = diFiles.GetFiles("*.*");
            List<SlideImage> lsImage = new List<SlideImage>();
            foreach (var fileImage in listFile)
            {
                if (fileImage.Extension != ".png" || fileImage.Extension != ".jpg")
                {
                    SlideImage sl = new SlideImage() { tenFile = fileImage.Name, URL = "../../../Upload/SlideImage/" + fileImage.Name };
                    lsImage.Add(sl);
                }
            }
            gridAnh.DataSource = lsImage;
            gridAnh.DataBind();
        }
        protected void Delete(object sender, CommandEventArgs e)
        {
            File.Delete(Server.MapPath("~/Upload/SlideImage/" + e.CommandArgument.ToString()));
            DisplaySlideImage();
        }
    }
}